package top.jasonlam.car.entity;

import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.lang.NonNull;

import javax.persistence.*;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.Date;
import java.util.Set;

/**
 * Car  汽车发布单
 * 因为汽车与用户的关系是依赖关系 没有用户就没有汽车 所以不将汽车单独放一个类
 * username     级联索引
 * commentSet   级联
 * status   发布状态
 * describe 出售信息
 * img      汽车图片
 * price    汽车价格
 * issueDate  发布单发布日期
 *
 * @author jasonlam
 */

@Table(name = "car")
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Car {
    @Id
    @GenericGenerator(name = "idGenerator", strategy = "org.hibernate.id.UUIDGenerator")
    @GeneratedValue(generator = "idGenerator")
    private String uuid;

    @Column(name = "title", nullable = false, length = 30)
    @NotEmpty(message = "发布标题不能为空")
    @Size(min = 1, max = 30)
    private String title;

    @Lob
    @Column(name = "cardescribe", columnDefinition = "text", nullable = false, length = 100)
    @NotEmpty(message = "汽车描述不能为空")
    @Size(min = 1, max = 300)
    private String describe;

    @Column(name = "price", nullable = false)
    @NotNull(message = "汽车价格不能为空")
    private Double price;

    @Column(name = "carstatus", nullable = false, length = 20)
    @NotNull(message = "汽车状态不能为空")
    @Min(value = -1, message = "状态值不合法")
    @Max(value = 2, message = "状态值不合法")
    private Integer status;

    @Lob
    @Column(name = "img", columnDefinition = "text")
    private String img;

    @Column(name = "username", nullable = false, length = 20)
    @NotEmpty(message = "汽车所属人账号不能为空")
    private String username;

    @Column(name = "issuedate")
    @Valid
    @CreationTimestamp
    private Date issueDate;

    @OneToMany(targetEntity = Comment.class, cascade = {CascadeType.ALL}, mappedBy = "car")
    Set<Comment> commentSet;
}
